package zsc.ruanc.practicaltraining.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import zsc.ruanc.practicaltraining.model.result.JsonResult;
import zsc.ruanc.practicaltraining.service.ClassificationService;
import zsc.ruanc.practicaltraining.utils.ResultUtils;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

/**
 *  文章分类操作 控制器
 */

@RestController
@RequestMapping("/class")
@Api(tags = "分类操作模块")
@Validated
public class ClassificationController {

    @Autowired
    private ClassificationService classificationService;


    @GetMapping("/selectPage")
    @ApiOperation(value = "获得全部信息(分页)", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cur", value = "当前页", dataType = "Integer", paramType = "query", defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "页大小", dataType = "Integer", paramType = "query", defaultValue = "10")
    })
    public JsonResult selectAll(@Min(message = "参数cur不能小于0", value = 0) @RequestParam(value = "cur", defaultValue = "1") Integer cur,
                                @Min(message = "参数size不能小于1", value = 1) @RequestParam(value = "size", defaultValue = "10") Integer size) {
        return ResultUtils.success(classificationService.selectPage(cur, size));
    }

    @GetMapping("/getNameList")
    @ApiOperation(value = "获得全部分类名", httpMethod = "GET")
    public JsonResult getNameList() {
        return ResultUtils.success(classificationService.getNameList());
    }

    @GetMapping("/selectByName")
    @ApiOperation(value = "按分类名查找", httpMethod = "GET")
    public JsonResult selectByName(@NotBlank(message = "条件不能为空") @RequestParam(value = "name", required = false) String name) {
        return ResultUtils.success(classificationService.selectByName(name));
    }






    //-----------管理员权限----------------------------------
    @GetMapping("/insert")
    @ApiOperation(value = "新增分类(管理员)", httpMethod = "GET")
    public JsonResult insert(@NotBlank(message = "不能为空") @RequestParam(value = "name", required = false) String className) {
        return ResultUtils.success(classificationService.insert(className), "新增成功");
    }

    @GetMapping("/update")
    @ApiOperation(value = "修改分类名（管理员）", httpMethod = "GET")
    public JsonResult update(@NotBlank(message = "name不能为空") @RequestParam(value = "name", required = false) String className,
                             @NotNull(message = "id不能为空") @Min(message = "id要大于0", value = 0) @RequestParam(value = "id", required = false) Integer id) {
        return ResultUtils.success(classificationService.update(id, className));
    }

    @GetMapping("/delete")
    @ApiOperation(value = "删除分类名（管理员）", httpMethod = "GET")
    public JsonResult update(@NotBlank(message = "name不能为空") @RequestParam(value = "name", required = false) String className) {
        return ResultUtils.success(classificationService.deleteByName(className));
    }


}
